########################
#ECtHR Matching
#set Working directory before running
#Use "ECtHRMatchingData.dta" (Stata v. 13)
#######################

#check if packages are installed, and if not, install them
packages <- c("foreign", "readstata13", "MatchIt")
if (length(setdiff(packages, rownames(installed.packages()))) > 0) {
  install.packages(setdiff(packages, rownames(installed.packages())), repos="http://cran.rstudio.com/")  
}
install.packages("Zelig")
library(Zelig)

#attach packages
lapply(packages, library, character.only = T)

#default the stringsAsFactors argument to FALSE for all functions with that argument
options(stringsAsFactors=FALSE)

#turn off scientific notation
options(scipen=999)

#set seed
set.seed(33603)

#set working directory
#setwd()

#load data
d <- read.dta13("ECtHRMatchingData.dta")


#keep the variables we want for analysis
data <- d[, c("ccode", "year", "repressionlatent", "numcaseimpk12jud_1", "numcase_impk12_1", "ecasebinimpk12_1", "lji_1", "lfdi_1", "speech_1", "cs_1", "nhri_1", "polconiii_1", "xrcomp_1", "cwar_1", "v2xel_frefair_1", "popmillionslog_1", "gdpcaplog_1", "regionalcountimpk12bin_1")]


#listwise delete
data1 <- na.omit(data)

#attach data
attach(data1)

#########################################################
#Matching - Main Model

#Nearest Neighbor (one to one)
nearest.out1<-matchit(ecasebinimpk12_1 ~ lji_1 + lfdi_1 + speech_1 + cs_1 + nhri_1 + polconiii_1 + xrcomp_1 + cwar_1 + v2xel_frefair_1 + popmillionslog_1 + gdpcaplog_1 + regionalcountimpk12bin_1, data = data1, method ="nearest", distance = "logit", discard = "both", reestimate = TRUE)

##Table 5##
nearest.out1
summary(nearest.out1)

#Summary of balance for all data:
#  Means Treated Means Control SD Control Mean Diff eQQ Med eQQ Mean eQQ Max
#distance                        0.4999        0.2364     0.1867    0.2635  0.2933   0.2638  0.3355
#lji_1                           0.7539        0.8193     0.2012   -0.0654  0.0351   0.0664  0.2268
#lfdi_1                          1.1490        0.6961     1.2155    0.4529  0.4702   0.4829  2.7172
#speech_1                        1.2231        1.4896     0.5404   -0.2665  0.0000   0.2629  1.0000
#cs_1                            0.8187        0.8260     0.1606   -0.0074  0.0104   0.0146  0.1606
#nhri_1                          0.6853        0.6181     0.4863    0.0671  0.0000   0.0677  1.0000
#polconiii_1                     0.4445        0.4312     0.1376    0.0133  0.0067   0.0211  0.1600
#xrcomp_1                        2.9163        2.8526     0.4048    0.0638  0.0000   0.0677  1.0000
#cwar_1                          0.1116        0.0756     0.2646    0.0359  0.0000   0.0359  1.0000
#v2xel_frefair_1                 0.8588        0.8689     0.2029   -0.0100  0.0118   0.0262  0.2045
#popmillionslog_1                2.8011        2.0923     1.1490    0.7088  0.5586   0.7130  1.7068
#gdpcaplog_1                     9.4271        9.2129     1.1977    0.2142  0.1975   0.2438  0.8090
#regionalcountimpk12bin_1        1.0000        0.9055     0.2928    0.0945  0.0000   0.0956  1.0000


#Summary of balance for matched data:
#  Means Treated Means Control SD Control Mean Diff eQQ Med eQQ Mean eQQ Max
#distance                        0.4773        0.3922     0.1595    0.0852  0.0847   0.0853  0.1986
#lji_1                           0.7639        0.7908     0.2181   -0.0268  0.0190   0.0395  0.2455
#lfdi_1                          1.1580        1.0016     1.1219    0.1564  0.2367   0.2484  3.4875
#speech_1                        1.2563        1.2941     0.5333   -0.0378  0.0000   0.0378  1.0000
#cs_1                            0.8207        0.8276     0.1540   -0.0069  0.0116   0.0159  0.0518
#nhri_1                          0.6765        0.6849     0.4655   -0.0084  0.0000   0.0084  1.0000
#polconiii_1                     0.4439        0.4497     0.1342   -0.0058  0.0133   0.0179  0.1267
#xrcomp_1                        2.9244        2.8908     0.3852    0.0336  0.0000   0.0336  1.0000
#cwar_1                          0.1050        0.0966     0.2961    0.0084  0.0000   0.0084  1.0000
#v2xel_frefair_1                 0.8619        0.8703     0.2105   -0.0084  0.0122   0.0303  0.2230
#popmillionslog_1                2.7173        2.5181     1.0455    0.1992  0.1855   0.3003  1.2839
#gdpcaplog_1                     9.4026        9.4253     1.2466   -0.0227  0.1411   0.1704  0.5652
#regionalcountimpk12bin_1        1.0000        1.0000     0.0000    0.0000  0.0000   0.0000  0.0000

#Percent Balance Improvement:
#  Mean Diff.  eQQ Med eQQ Mean  eQQ Max
#distance                    67.6798  71.1077  67.6436  40.7939
#lji_1                       58.9957  45.7265  40.4598  -8.2451
#lfdi_1                      65.4742  49.6570  48.5524 -28.3462
#speech_1                    85.8102   0.0000  85.6188   0.0000
#cs_1                         5.8206 -11.3163  -9.0764  67.7456
#nhri_1                      87.4785   0.0000  87.5927   0.0000
#polconiii_1                 56.6480 -99.9987  15.2923  20.8333
#xrcomp_1                    47.3000   0.0000  50.3707   0.0000
#cwar_1                      76.6180   0.0000  76.5640   0.0000
#v2xel_frefair_1             16.5943  -3.0770 -15.4166  -9.0369
#popmillionslog_1            71.8932  66.7907  57.8894  24.7747
#gdpcaplog_1                 89.3893  28.5386  30.1112  30.1358
#regionalcountimpk12bin_1   100.0000   0.0000 100.0000 100.0000

#Sample sizes:
#  Control Treated
#All           529     251
#Matched       238     238
#Unmatched     215       0
#Discarded      76      13

#create data frame
nearest.data1<-match.data(nearest.out1)

#Regression
reg<-zelig(repressionlatent ~ numcaseimpk12jud_1 + numcase_impk12_1 + lji_1 + lfdi_1 + speech_1 + cs_1 + nhri_1 + polconiii_1 + xrcomp_1 + cwar_1 + v2xel_frefair_1 + popmillionslog_1 + gdpcaplog_1 + regionalcountimpk12bin_1, data = nearest.data1, model= "normal")


##Table 6##
summary(reg)

#Estimate Std. Error t value          Pr(>|t|)
#numcaseimpk12jud_1        0.06000    0.04275   1.404          0.161081
#numcase_impk12_1         -0.04879    0.02859  -1.706          0.088593
#lji_1                     1.41475    0.31058   4.555 0.000006704900480








